<?php 
class TypeModel extends Model
{
	//设置表名
	protected $trueTableName = 'ty_type';
	
	//设置主键
	protected  $key = 'ty_id';
	

	/**
	 * 通过ID 获取一条数据
	 * @param unknown $id 主键ID
	 * @return Ambigous <mixed, boolean, NULL, multitype:, unknown, string>
	 */
	public function getRowById($id)
	{
			
		//字段
		$field = array(
	
				'ty_id',
				'title',
				'order',
				'pid',
                 'is_show',
                'addtime'
	
		);
	
		//条件
		$where = $this->key."=".$id;
			
		return $this->field($field)->where($where)->find();
			
	}

	//获取数据条数
	public function getCount($seek='')
	{
	
		//条件
		$where = '1=1';
	
		return $this->where($seek.$where)->count();
	
	}
	/**
	 * 获取列表
	 * @param unknown $page 分页参数
	 * @param unknown $seek 搜索参数
	 * @return Ambigous <mixed, string, boolean, NULL, unknown, multitype:, multitype:multitype: , void, object>
	 */
	public function getListAll($page, $seek='')
	{
		//字段
		$field = array(
				'*',
		);

		//条件
		$where = '1=1';

		return $this->field($field)->where($seek.$where)->order('ty_id asc')->limit($page)->select();
	
	}
	/**
	 * 
	 * @param unknown $id
	 * @return Ambigous <mixed, boolean, NULL, multitype:, unknown, string>
	 */
	public function getRowByNode($id)
	{
			
		//获取父级id
		$row = $this->getRowById($id);
			
		$id = $row['pid'];
			
		//字段
		$field = array(
					
				'title',
				'ty_id',
	
		);
	
		//条件
		$where = $this->key."='{$id}'";
	
		return $this->field($field)->where($where)->find();
			
	}
	/**
	 * 获取所有子节点
	 * @param unknown $id
	 * @return Ambigous <mixed, boolean, NULL, string, unknown, multitype:, multitype:multitype: , void, object>
	 */
	public function getNode($id)
	{
			
		//字段
		$field = array(
					
				'ty_id',
				'title'
	
		);
			
		//条件
		$where = "pid in (".$id.")";
	
		return $this->field($field)->where($where)->select();
			
	}
	
	/**
	 * 获取所有顶级分类列
	 * @return Ambigous <mixed, string, boolean, NULL, unknown, multitype:, multitype:multitype: , void, object>
	 */
	public function getListByPid()
	{
			
		//字段
		$field = array(
	
				'ty_id',
				'title'
			
		);
			
		//条件
		$where = 'pid=0';
			
		return $this->field($field)->where($where)->select();
			
	}
    /**
     * 插入一条数据
     * @param array $data
     * @return Ambigous <mixed, boolean, string, false, number>
     */
    public function insert(array $data)
    {
        //字段
        $field = array(
            'ty_id',
            'title',
            'order',
            'pid',
            'is_show',
            'addtime'
        );

        //字段过滤
        $data = $this->CheckField($data, $field);

        //添加时间
        $data['add_time'] = time();

        return $this->add($data);

    }
    /**
     * 更新一条数据
     */
    public function update(array $data, $id)
    {

        //字段
        $field = array(
            'ty_id',
            'title',
            'order',
            'is_show',
            'pid',
            'addtime'
        );

        //字段过滤
        $data = $this->CheckField($data, $field);

        //条件
        $where = $this->key.'='.$id;


        return $this->where($where)->save($data);

    }
    /**
     * 删除控制
     * @param unknown $id
     * @return Ambigous <mixed, boolean, false, number>
     */
    public function del($id)
    {

        //单选
        if ( !is_array($id) )
        {

            return $this->delete($id);
        }
        //多选
        else
        {

            $idStr = implode(',', $id);
            //die($idStr);
            return $this->where("ty_id in (".$idStr.")")->delete();

        }

    }


}


?>